import { NestFactory } from '@nestjs/core';
import { Logger } from '@nestjs/common';
import { AppModule } from './app.module';
import { CommonConfigRegister } from './modules/config/registers/common.register';
import * as helmet from 'helmet';
import { NestExpressApplication } from '@nestjs/platform-express';
import { generateSwagger } from './service';
import { ValidationPipe } from './modules/common/pipes/validation.pipe';

import * as express from 'express';
import { ConfigType } from '@nestjs/config';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  const commonConfig = app.select(AppModule).get<ConfigType<typeof CommonConfigRegister>>(CommonConfigRegister.KEY);

  //允许跨域请求
  app.enableCors();

  //使用helmet进行安全防护，文档详见：https://helmetjs.github.io/docs/
  app.use(helmet());

  // 设备body参数大小
  app.use(express.urlencoded({ limit: '3mb', extended: true }));
  app.use(express.json({ limit: '10mb' }));

  // 去掉http请求头中的X-Powered-By值
  app.disable('x-powered-by');

  //生成swagger
  if (commonConfig.enableSwagger) generateSwagger(app);

  // 全局使用管道(数据校验)
  app.useGlobalPipes(new ValidationPipe());

  await app.listen(commonConfig.port);

  Logger.log(`服务已经启动:${await app.getUrl()}`);
}
bootstrap();
